﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace ChipSharp
{
    class CharpMan : CommonMovable
    {
        const double myStepSpeed = 4.0; /* 4 blocks per seconds */
        const double myIceSpeed = 16.0;

        public bool Controllable { get; set; }

        // inventory
        public bool WithIceBoot { get; set; }
        public bool WithWaterBoot { get; set; }

        protected bool myAlive = true;


        public CharpMan(int x, int y)
            : base(x, y)
        {
            Controllable = true;

            WithIceBoot = false;
            WithWaterBoot = true;
        }

        public void StartMoving(World.Direction dir)
        {
            IntendedDir = dir;
        }

        public void StopMoving()
        {
            IntendedDir = World.Direction.kNone;
        }

        protected double GetStepTicks(Game game)
        {
            int ti = game.TimeInc;
            int ts = game.TimeSpan;
            return (double)ts / (ti * myStepSpeed);
        }

        protected double GetIceTicks(Game game)
        {
            int ti = game.TimeInc;
            int ts = game.TimeSpan;
            return (double)ts / (ti * myIceSpeed);
        }

        public override void Step(World world)
        {
            Charp tileObj = world.Map[Y, X];

            if (! myAlive || tileObj is Water && !WithWaterBoot)
            {   // dead
                if (myAlive)
                {
                    world.Game.SendCommand(Game.CommandType.kDie);
                }
                myAlive = false;
                return;
            }

            if (tileObj is Ice && !WithIceBoot)
            {
                Controllable = false;
                base.TicksToNext = GetIceTicks(world.Game);
                if (!myOnIce)
                {
                    base.TicksRemaining = 0.0;
                    myOnIce = true;
                }
                IntendedDir = World.Direction.kNone;
            }
            else
            {
                myOnIce = false;
                Controllable = true;
            }

            if (Controllable)
            {
                if (IntendedDir == World.Direction.kNone)
                {
                    base.VDir = IntendedDir;
                    base.TicksToNext = 0.0;
                }
                else
                {
                    if (base.VDir != IntendedDir)
                    {
                        base.TicksRemaining = 0.0;
                        base.VDir = IntendedDir;
                    }
                    base.TicksToNext = GetStepTicks(world.Game);
                }
            }

            base.Step(world);
        }

        public override void Draw(System.Drawing.Graphics graph, System.Drawing.Rectangle rect)
        {
            Pen pen = new Pen(Color.Black);
            SolidBrush brush = new SolidBrush(Color.Green);

            if (myAlive)
            {
                graph.FillEllipse(brush, rect);
                graph.DrawEllipse(pen, rect);
            }

            base.Draw(graph, rect);
        }

        protected World.Direction IntendedDir = World.Direction.kNone;
        protected bool myOnIce = false;

    }
}
